#ifndef _SERIALIZE_STREAM_H_
#define _SERIALIZE_STREAM_H_

#include "Common/BaseType.h"
#include "Common/Unit/Stream/Bytesbuffer.h"
#include "Common/Unit/TimeManager.h"
#include "Common/Unit/Exception.h"
#include "Common/Unit/Stream/Serializeexception.h"
#include "Common/Unit/Assertx.h"

#include <string>
#include <vector>

#define SERIALIZE_MAX_RESIZE		102400

namespace Common
{
	class CSerializeStream
		:public CBytesBuffer
	{
	public:
		typedef CBytesBuffer surper;
		CSerializeStream();
		~CSerializeStream();
		/**
		* @param buf point to buffer
		* @param length data size
		*/
		VOID append(const VOID* buf ,INT length );
		/**
		* resize to buffer
		* @param to resize the size
		*/
		VOID resize( INT size );
		/**
		* reset
		*/
		VOID clear();
		/**
		* size data operation
		*/
		VOID writeSize(INT);
		VOID readSize(INT&);

		/**
		* string data operation
		*/
		VOID writeTypeId(const std::string& str);
		VOID readTypeId( std::string & str);

		/**
		* byte data operation
		*/
		VOID write(BYTE v);
		VOID write(const std::vector<BYTE>&);
		VOID read(BYTE& v);
		VOID read( std::vector<BYTE>&);

// 		/**
// 		* bool data operation BOOL is typedf of INT
// 		*/
// 		VOID write(BOOL v);
// 		VOID write(const std::vector<BOOL>&);
// 		VOID read(BOOL& v);
// 		VOID read(std::vector<BOOL>&);

		/**
		* short data operation
		*/
		VOID write(SHORT);
		VOID write(const std::vector<SHORT>&);
		VOID read(SHORT&);
		VOID read(std::vector<SHORT>&);

		/**
		* INT data operation
		*/
		VOID write(INT);
		VOID write(const std::vector<INT>&);
		VOID read(INT&);
		VOID read(std::vector<INT>&);

		/**
		* LONG64 data operation
		*/
		VOID write(LONG64);
		VOID write(const std::vector<LONG64>&);
		VOID read(LONG64&);
		VOID read(std::vector<LONG64>&);

		/**
		* float data operation
		*/
		VOID write(FLOAT);
		VOID write(const std::vector<FLOAT>&);
		VOID read(FLOAT&);
		VOID read(std::vector<FLOAT>&);

		/**
		* double data operation
		*/
		VOID write(DOUBLE);
		VOID write(const std::vector<DOUBLE>&);
		VOID read(DOUBLE&);
		VOID read(std::vector<DOUBLE>&);

		/**
		* string data operation
		*/
		VOID write(const std::string&);
		VOID write(const std::vector<std::string>&);
		VOID read(std::string&);
		VOID read(std::vector<std::string>&);

		/**
		* wstring data operation
		*/
		VOID write(const std::wstring&);
		VOID write(const std::vector<std::wstring>&);
		VOID read(std::wstring&);
		VOID read(std::vector<std::wstring>&);

		/**
		* date time data operation
		*/
		VOID write(const CDateTime&);
		VOID write(const std::vector<CDateTime>&);
		VOID read(CDateTime&);
		VOID read(std::vector<CDateTime>&);

		/**
		* Seq data operation
		*/
// 		VOID startSeq(INT, INT);
// 		VOID checkSeq();
// 		VOID checkSeq(INT);
// 		VOID checkFixedSeq(INT, INT); // For sequences of fixed-size types.
// 		VOID endElement()
// 		{
// 			Assert(_seqDataStack);
// 			--_seqDataStack->numElements;
// 		}
// 		VOID endSeq(INT);

		VOID write( const Common::CException& ex );
		VOID read( Common::CException& ex );

		INT getBytesLeft() const;
		char* getReadPoint() const;
	protected:
		VOID addReadPoint( INT nLen );
		INT _readIndex;
		VOID checkReadLength( 
			INT readLength , 
			const char* name , 
			const char * file , 
			const INT line 
			);
// 		struct SeqData
// 		{
// 			SeqData(INT, INT);
// 			INT numElements;
// 			INT minSize;
// 			SeqData* previous;
// 		};
// 		SeqData* _seqDataStack;
	private:
		CSerializeStream( const CSerializeStream& );
		CSerializeStream& operator=( const CSerializeStream& other );
	};
}
#endif